#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "user.h"
#define USER_FILE "user.txt"


int write_user(User *r)
{
	FILE *fp;

	fp = fopen(USER_FILE,"a");
	if (fp == NULL)
	{
		perror("open user file");
		return 1;
	}
	fprintf(fp,"%-12s",r->userId);
	fprintf(fp, "%-7s",r->password);
	fprintf(fp, "%-9s",r->name);
	fprintf(fp, "%-4s",r->sex);
	fprintf(fp, "%-8s",r->role);
	fprintf(fp, "%-20s\n",r->email);
	if (fp)
		fclose(fp);
	return 0;

}

int write_user_all(User *r)  //form head
{
	FILE *fp;

	fp = fopen(USER_FILE,"w");
	if (fp == NULL)
	{
		perror("open user file");
		return 1;
	}
	while(r->next)
	{
		fprintf(fp,"%-12s",r->userId);
		fprintf(fp, "%-7s",r->password);
		fprintf(fp, "%-9s",r->name);
		fprintf(fp, "%-4s",r->sex);
		fprintf(fp, "%-8s",r->role);
		fprintf(fp, "%-20s\n",r->email);
		r = r->next;
	}	
	if (fp)
		fclose(fp);
	return 0;

}

User* read_user()
{
	User *head,*p,*newNode,*tp;
	FILE *fp;
	fp = fopen (USER_FILE,"r");
	if (fp == NULL)
	{
		perror("open user file read");
		return NULL;
	}
	head = (User *)malloc(sizeof(User));
	if (NULL == head)
	{
		printf("malloc fail\n");
		if(fp) fclose(fp);
		return NULL;
	}
	memset(head,0,sizeof(User));
	head->next = NULL;
	p = head;
	while(!feof(fp))
	{
		fscanf(fp,"%s",p->userId);
		fscanf(fp,"%s",p->password);
		fscanf(fp, "%s",p->name);
		fscanf(fp, "%s",p->sex);
		fscanf(fp, "%s",p->role);
		fscanf(fp, "%s",p->email);

	//	printf("read one node\n");
	//	printf("UserId :%s",p->userId);

		newNode = (User*)malloc(sizeof(User));
		memset(newNode,0,sizeof(User));
		p->next = newNode;
		tp = p;
		p = newNode;
	}
	tp->next = NULL;
	free (p); 
	if(fp) fclose(fp);
	return head;
}

int display_user(User *head)
{
	User *p;
	int num = 1;
	p = head;
	printf("##########################\n");
	printf("Num  ID          Password   Name     Sex  Role      Emali\n");
	while(p->next)
	{	
		printf("%2d.  %-12s%-7s    %-9s%-2s   %-8s  %-20s\n",num,p->userId,p->password,p->name,p->sex,p->role,p->email);
		p = p->next;
		num++;
	}
	printf("##########################\n");
	
	return 0;
}
int add_user()
{
	User *pr;
	char *role_str[4] = {"admin","manager","recept","cleaner"};
	pr = (User*)malloc(sizeof(User));
		if(pr == NULL)
		{
			return 1;
		}
		memset(pr,0,sizeof(User));
		printf("请输入 userId:\n");
		fgets(pr->userId,12,stdin);
		printf("请输入 password:\n");
		fgets(pr->password,7,stdin);
		printf("请选择一个用户权限:1.admin,2.manager,3.recept,4.cleaner\n");
		int s;
		scanf("%d",&s);
		getchar();
		while(s < 1|| s > 4)
		{
			printf("选择错误，请重新输入:\n");
			scanf("%d",&s);
			getchar();
		}
		strcpy(pr->role,role_str[s-1]);
		printf("请输入 user name:\n");
		fgets(pr->name,9,stdin);
		printf("请输入 user email:\n");
		fgets(pr->email,20,stdin);
		printf("请输入 sex(m/f):\n");
		fgets(pr->sex,4,stdin);

		write_user(pr);
		free(pr);

		printf("用户添加成功\n");
		printf("输入任意指令以继续:\n");
		getchar();
		return 0;
}


int remove_user()
{
	
	int s,i;    //s for remove the num node 
	User *remove_head,*remove_p,*remove_tp;
	remove_head = read_user();
	display_user(remove_head);
	remove_p = remove_tp = remove_head;
	remove_p = remove_p->next;
	printf("请输入要删除的用户对应的编号 :\n");
	scanf("%d",&s);	
	getchar();
	for (i = 2;i < s;i++)
	{
		remove_p = remove_p->next;
		remove_tp = remove_tp->next;
	}
//	printf("  %-12s%-7s    %-9s\n",remove_p->userId,remove_p->password,remove_p->name);
	remove_tp->next = remove_p->next;   //to remove
	free (remove_p);

	write_user_all(remove_head);

	printf("用户删除成功\n");
	printf("输入任意指令以继续:\n");
	getchar();

	return 0;
}


void srtup_user_password()
{
	int i;
	char s[12],srtup_p_word[7] = "123456";
	User *head,*p;
	head = read_user();
	display_user(head);
	p = head ;
	printf("请输入要初始化密码用户对应的 userId :\n");
	scanf("%s",s);
	getchar();
	while(p->next != NULL)
	{
		i = strcmp(s,p->userId);
		if (i == 0)
			break;
		p = p->next;
	}
//	printf("name = %-7s ,password = %s,srtup_p_word = %s\n",p->name,p->password,srtup_p_word);	
	if (p->next == NULL)
	{
		printf("not find the userId!\n");
		printf("输入任意指令以继续: \n");
		getchar();
		return ;
	}
	strcpy(p->password,srtup_p_word);
	write_user_all(head);

	printf("初始化用户密码成功!\n");
	printf("输入任意指令以继续: \n");
	getchar();
	return ;
}

void edit_user_information()
{
		int i,choose;
		char id[12];
		User *head,*p;
		head = read_user();
		display_user(head);
		p = head ;
		printf("请输入要改变用户信息的用户对应的 userId:\n");
		scanf("%s",id);
		getchar();
		while(p->next != NULL)
		{
			i = strcmp(id,p->userId);
			if (i == 0)
				break;
			p = p->next;
		}
		
		if (p->next == NULL)
		{
			printf("not find the userId!\n");
			printf("输入任意指令以继续: \n");
			getchar();
			return ;
		}

		printf("请选择要改变的用户信息:\n ");
		printf("1.password  2.name  3.sex  4.role  5.email 0.(quit)\n");
		scanf("%d",&i);
		getchar();
		switch(i)
		{
			case 1:
				printf("输入 new password :\n");
				fgets(p->password,7,stdin);
				getchar();
				break;
			case 2:
				printf("输入 new name :\n");
				fgets(p->name,9,stdin);
				break;
			case 3:
				if (strcmp(p->sex,"m") == 0)
				{
					printf("now sex is m ,edit to f ?\n");
					printf("input 1/0 for yes/no \n");
					scanf("%d",&choose);
					getchar();
					if (choose == 1)
						strcpy(p->sex,"f");

				}
				else if (strcmp(p->sex,"f") == 0)
				{
					printf("now sex is f ,edit to m ?\n");
					printf("input 1/0 for yes/no \n");
					scanf("%d",&choose);
					getchar();
					if (choose == 1)
						strcpy(p->sex,"m");
				}
				break;
			case 4:
				printf("当前用户权限是 %s , 选择改变后的用户权限:\n ",p->role);
				printf("1.admin 2.manager 3.recept 4.cleaner 0.quit \n");
				scanf("%d",&choose);
				getchar();
				if (choose == 1)
					strcpy(p->role,"admin");
				else if (choose == 2)
					strcpy(p->role,"manager");
				else if (choose == 3)
					strcpy(p->role,"recept");
				else if (choose == 4)
					strcpy(p->role,"cleaner");

				break;
			case 5:
				printf("输入 new email :\n");
				fgets(p->email,20,stdin);
				break;
			case 0:
			default:
				break;
		}
		write_user_all(head);

		printf("修改用户信息成功\n");
		printf("输入任意指令以继续: \n");
		getchar();
		return ;
}

void change_my_userinf(User *r)
{
	User *head,*p;
	head = read_user();
	p = head;
	int i,choose;
	while(p->next != NULL)
	{
		i = strcmp(r->userId,p->userId);
		if (i == 0)
			break;
		p = p->next;
	}
			
	if (p->next == NULL)
	{
		printf("not find the userId!\n");
		printf("输入任意指令以继续: \n");
		getchar();
		return ;
	}
	printf("##########################\n");
	printf("Num  ID          Password   Name     Sex  Role      Emali\n");		
	printf("01.  %-12s%-7s    %-9s%-2s   %-8s  %-20s\n",p->userId,p->password,p->name,p->sex,p->role,p->email);			
	printf("##########################\n");
	printf("请选择要改变的用户信息:\n ");
	printf("1.password  2.name  3.sex  4.email 0.(quit)\n");
	scanf("%d",&i);
	getchar();
	switch(i)
	{
		case 1:
			printf("输入 new password :\n");
			fgets(p->password,7,stdin);
			getchar();
			break;
		case 2:
			printf("输入 new name :\n");
			fgets(p->name,9,stdin);
			break;
		case 3:
			if (strcmp(p->sex,"m") == 0)
			{
				printf("now sex is m ,edit to f ?\n");
				printf("input 1/0 for yes/no \n");
				scanf("%d",&choose);
				getchar();
				if (choose == 1)
					strcpy(p->sex,"f");
			}
			else if (strcmp(p->sex,"f") == 0)
			{
				printf("now sex is f ,edit to m ?\n");
				printf("input 1/0 for yes/no \n");
				scanf("%d",&choose);
				getchar();
				if (choose == 1)
					strcpy(p->sex,"m");
			}
			break;
		case 4:
			printf("输入 new email :\n");
			fgets(p->email,20,stdin);
			break;
		case 0:
		default:
			break;
	}
	write_user_all(head);

	printf("修改个人信息成功\n");
	printf("输入任意指令以继续: \n");
	getchar();
	return ;
}

